package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.mapper.WorkBankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IWorkerBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;


@Service
public class IWorkerBankAccountServiceImpl extends ServiceImpl<WorkBankAccountMapper, BankAccount> implements IWorkerBankAccountService {



    //("新增银行账户")
    @Override
    public void addOrUpdate(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        //获取当前登录的用户id
        Long userId = UserContext.currentUserId();
        //判断是添加还是更新  根据user_id能不能查询到这个数据
        BankAccount bankAccount = lambdaQuery().eq(BankAccount::getUserId, userId).one();
        if (ObjectUtil.isNull(bankAccount)){ //为空 添加数据
            BankAccount bankAccount1 = new BankAccount();
            BankAccount bankAccount2 = BeanUtils.toBean(bankAccountUpsertReqDTO, bankAccount1.getClass());
            bankAccount2.setUserId(userId);
            baseMapper.insert(bankAccount2);
        }else {
            BankAccount bankAccount3 = new BankAccount();
            BankAccount bankAccount4 = BeanUtils.toBean(bankAccountUpsertReqDTO, bankAccount3.getClass());
            bankAccount4.setUserId(userId);
            lambdaUpdate().eq(BankAccount::getUserId,userId).update(bankAccount4);
        }
    }


    //=========================================("获取用户的银行账户")=============================
    @Override
    public BankAccountResDTO getBankAccount() {
        //获取当前登录的用户id
        Long userId = UserContext.currentUserId();
        BankAccount bankAccount = lambdaQuery().eq(BankAccount::getUserId, userId).one();
        if (ObjectUtils.isNull(bankAccount)){
            throw new CommonException("用户账户不存在");
        }
        BankAccountResDTO bankAccountResDTO = new BankAccountResDTO();
        BankAccountResDTO bankAccountResDTO1 = BeanUtils.copyBean(bankAccount, bankAccountResDTO.getClass());



        return bankAccountResDTO1;
    }
}
